package com.atguigu.srb.core.controller.api;

import com.atguigu.common.exception.BusinessAssert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.controller.hbf.RequestHelper;
import com.atguigu.srb.core.pojo.vo.UserBindVO;
import com.atguigu.srb.core.service.UserBindService;
import com.atguigu.srb.utils.JwtUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: srb
 * @author: 龙龙
 * @create: 2022-02-21 10:31
 * @description:
 **/
@Api(tags = "会员账号绑定")
@RestController
@RequestMapping("/api/core/userBind")
@Slf4j
public class ApiUserBindController {
    @Autowired
    private UserBindService userBindService;

    @PostMapping("/auth/bind")
    public R bind( HttpServletRequest request , @RequestBody UserBindVO userBindVO) {
        //汇付宝提交表单
        String from = "";
        System.out.println(userBindVO);

        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

       String formStr = userBindService.bind(userBindVO,userId);

        return R.ok().data("formStr",formStr);
    }


    @RequestMapping("/notify")
    public String notifySrb(HttpServletRequest request){
        //验签
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = RequestHelper.switchMap(parameterMap);
        boolean signEquals = RequestHelper.isSignEquals(map);

        BusinessAssert.isTrue(signEquals, ResponseEnum.WEIXIN_CALLBACK_PARAM_ERROR);
        userBindService.notifySrb(map);
        return "success";

    }
}
